package com.compty.paint.manager;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.compty.core.hibernate.HibernateEntityDao;
import com.compty.core.page.Page;
import com.compty.paint.domain.AuditState;
import com.compty.paint.domain.Exhibition;
import com.compty.paint.domain.UploadState;
import com.compty.paint.util.Constants;

public class ExhibitionManager extends HibernateEntityDao<Exhibition>{
	
	private UploadStateManager uploadStateManager;
	private AuditStateManager auditStateManager;
	
	public void setUploadStateManager(UploadStateManager uploadStateManager) {
		this.uploadStateManager = uploadStateManager;
	}

	public void setAuditStateManager(AuditStateManager auditStateManager) {
		this.auditStateManager = auditStateManager;
	}

	/**
	 *@Todo:由展览ID查询作品
	 *@author:zhuqh
	 *@CreateTime:2011-8-18 上午11:27:12
	 * @param exhibitionId 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List findPaintingsByExhibitionId(Serializable exhibitionId){
		
		String hql = "select p from Exhibition e inner join e.paintings p  where e.id=?";
//		String hql = "select e.paintings from Exhibition e  where e.id=?";
		
		List list = find(hql, exhibitionId);
		
		return list;
	}

	/**
	 *@Todo:查找全国展览
	 *@author:zhuqh
	 *@CreateTime:2011-8-18 上午11:23:36
	 * @param count 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Exhibition> findGlobalExhibitionList(int count) {
	
		Criteria criteria = createCriteria();
		
		criteria.addOrder(Order.desc("id"));
		
        UploadState  uploadState = uploadStateManager.findUniqueBy("english_name", Constants.Upload_State_Init); //上传状态正常
        criteria = criteria.add(Restrictions.eq("uploadState", uploadState)); 
        AuditState auditState = auditStateManager.findUniqueBy("english_name", Constants.Audit_State_Pass);  //审核状态通过
        criteria = criteria.add(Restrictions.eq("auditState", auditState)); 
        
        criteria.add(Restrictions.eq("type", "2"));//2:表示全国展览

		Page page = super.pagedQuery(criteria, 1,count);
		
		return (List<Exhibition>) page.getResult();
	}
	
	/**
	 *@Todo:分页查询
	 *@author:zhuqh
	 *@CreateTime:2011-8-18 上午11:31:21
	 * @param type //展览类型  1:表示网上展览，2:表示全国展览
	 * @param curPage 当前页码
	 * @param pageSize  显示条数
	 * @return
	 */
	public Page findByPage(String type,int curPage, int pageSize){
		
		Criteria criteria = createCriteria();
		criteria.addOrder(Order.desc("id"));
		
        UploadState  uploadState = uploadStateManager.findUniqueBy("english_name", Constants.Upload_State_Init); //上传状态正常
        criteria = criteria.add(Restrictions.eq("uploadState", uploadState)); 
        AuditState auditState = auditStateManager.findUniqueBy("english_name", Constants.Audit_State_Pass);  //审核状态通过
        criteria = criteria.add(Restrictions.eq("auditState", auditState)); 
        
        criteria.add(Restrictions.eq("type", type));

		return super.pagedQuery(criteria, curPage,pageSize);
		
	}

	/**
	 * 查询所有展览(不区分类别)
	 * @param curPage 当前页码
	 * @param pageSize 每页条数
	 * @return
	 */
	public Page findAllExhibitionList(Integer curPage, Integer pageSize) {
		
		Criteria criteria = createCriteria();
		criteria.addOrder(Order.desc("id"));
		
        UploadState  uploadState = uploadStateManager.findUniqueBy("english_name", Constants.Upload_State_Init); //上传状态正常
        criteria = criteria.add(Restrictions.eq("uploadState", uploadState)); 
        AuditState auditState = auditStateManager.findUniqueBy("english_name", Constants.Audit_State_Pass);  //审核状态通过
        criteria = criteria.add(Restrictions.eq("auditState", auditState)); 

		return super.pagedQuery(criteria, curPage,pageSize);
	}

	/**
	 * 根据省份查询所有展览(不区分类别)
	 * @param curPage 当前页码
	 * @param pageSize 每页条数
	 * @return
	 */
	public Page findExhibitionByProvince(Integer curPage, Integer pageSize,Long pId) {
		// TODO Auto-generated method stub
		Criteria criteria = createCriteria();
		criteria.addOrder(Order.desc("id"));
		
        UploadState  uploadState = uploadStateManager.findUniqueBy("english_name", Constants.Upload_State_Init); //上传状态正常
        criteria = criteria.add(Restrictions.eq("uploadState", uploadState)); 
        AuditState auditState = auditStateManager.findUniqueBy("english_name", Constants.Audit_State_Pass);  //审核状态通过
        criteria = criteria.add(Restrictions.eq("auditState", auditState)); 
        
        criteria = criteria.add(Restrictions.eq("provinceInfo.id", pId)); 

		return super.pagedQuery(criteria, curPage,pageSize);
	}
	
}
